package com.svse.test;

import com.svse.entity.UserEntity;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import org.springframework.util.CollectionUtils;
import sun.text.normalizer.UTF16;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

/**
 * @author LiYuhang
 * @version 0.1
 * @application   java
 * @Copyright 上海危网信息科技有限公司版权所有
 * @company wwsoft
 * @Date 2019/12/9 13:50
 */
@Slf4j
public class Java8 {
    @Test
    public void test01() {
        UserEntity user1 =new UserEntity();
        user1.setName("张三");
        String userName1 = Optional.ofNullable(user1.getName()).orElseGet(() -> "李四");
        log.info("userName1:{}",userName1);
        UserEntity user2 =new UserEntity();
        String userName2 = Optional.ofNullable(user2.getName()).orElseGet(() -> "李四");
        log.info("userName2:{}",userName2);

        UserEntity user3 =new UserEntity();
        String userName3 = Optional.ofNullable(user3.getName()).orElseGet(() -> "李四");
        log.info("userName3:{}",userName3);

        String userName4=  user3.getName()==null ? "李四" :user2.getName() ;
        log.info("userName4:{}",userName4);

    }
    @Test
    public void test02() {
        List<UserEntity> userEntityList=new ArrayList<UserEntity>();
        for(int i=1;i<4;i++){
            UserEntity userEntity=new UserEntity();
            userEntity.setName("测试账号"+i);
            userEntity.setAge(i);
            userEntityList.add(userEntity);
        }
        log.info("userEntityList一共有：{}条数据",userEntityList.size());
        userEntityList.forEach(value->{
            log.debug("循环输入数据,内容是:{}",value.toString());
        });
        log.debug("排序前的数据-----------------------");
        //第一种
        Collections.sort(userEntityList,
               //倒叙
               (u1,u2)->u2.getAge()-u1.getAge()
                //正序
                // (u1,u2)->u2.getAge()-u1.getAge()
        );
        //第二种 正序
        Collections.sort(userEntityList, Comparator.comparingInt(UserEntity::getAge));
        //倒叙
        Collections.sort(userEntityList, Comparator.comparingInt(UserEntity::getAge).reversed());
        //第三种
        userEntityList.sort(Comparator.comparingInt(UserEntity::getAge).reversed());
        userEntityList.sort(Comparator.comparingInt(UserEntity::getAge));

        userEntityList.forEach(value->{
            log.debug("循环输入数据,内容是:{}",value.toString());
        });
        log.debug("排序后的数据-----------------------");
    }


    @Test
    public void test03(){
        //old
        new Thread((new Runnable() {
            @Override
            public void run() {
                System.out.println("匿名内部类 实现线程");
            }
        })).start();
        //lambda
        new Thread( () -> System.out.println("java8 lambda实现线程")).start();
    }

}
